function e(e) {
  for (var t = {}, r = e.split(","), s = 0; s < r.length; s++) {
    t[r[s]] = true;
  }
  return t;
}
var t = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/;
var r = /^<\/([-A-Za-z0-9_]+)[^>]*>/;
var s = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g;
var a = e("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr");
var n = e("a,address,code,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video");
var i = e("abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var");
var o = e("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr");
var l = e("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected");
var c = e("wxxxcode-style,script,style,view,scroll-view,block");
module.exports = function (e, d) {
  function f(e, t) {
    if (t) {
      for (t = t.toLowerCase(), r = b.length - 1; r >= 0 && b[r] != t; r--) {
        ;
      }
    } else {
      var r = 0;
    }
    if (r >= 0) {
      for (var s = b.length - 1; s >= r; s--) {
        if (d.end) {
          d.end(b[s]);
        }
      }
      b.length = r;
    }
  }
  var p;
  var u;
  var h;
  var b = [];
  var m = e;
  for (b.last = function () {
    return this[this.length - 1];
  }; e;) {
    u = true;
    if (b.last() && c[b.last()]) {
      e = e.replace(new RegExp("([\\s\\S]*?)</" + b.last() + "[^>]*>"), function (e, t) {
        t = t.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g, "$1$2");
        if (d.chars) {
          d.chars(t);
        }
        return "";
      });
      f(0, b.last());
    } else {
      if (0 == e.indexOf("<!--")) {
        if ((p = e.indexOf("-->")) >= 0) {
          d.comment && d.comment(e.substring(4, p)), e = e.substring(p + 3), u = false;
        }
      } else {
        0 == e.indexOf("</") ? (h = e.match(r)) && (e = e.substring(h[0].length), h[0].replace(r, f), u = false) : 0 == e.indexOf("<") && (h = e.match(t)) && (e = e.substring(h[0].length), h[0].replace(t, function (e, t, r, c) {
          t = t.toLowerCase();
          if (n[t]) {
            for (; b.last() && i[b.last()];) {
              f(0, b.last());
            }
          }
          if (o[t] && b.last() == t) {
            f(0, t);
          }
          (c = a[t] || !!c) || b.push(t);
          if (d.start) {
            var p = [];
            r.replace(s, function (e, t) {
              var r = arguments[2] ? arguments[2] : arguments[3] ? arguments[3] : arguments[4] ? arguments[4] : l[t] ? t : "";
              p.push({
                name: t,
                value: r,
                escaped: r.replace(/(^|[^\\])"/g, '$1\\"')
              });
            });
            if (d.start) {
              d.start(t, p, c);
            }
          }
        }), u = false);
      }
      if (u) {
        p = e.indexOf("<");
        for (var g = ""; 0 === p;) {
          g += "<";
          p = (e = e.substring(1)).indexOf("<");
        }
        g += p < 0 ? e : e.substring(0, p);
        e = p < 0 ? "" : e.substring(p);
        if (d.chars) {
          d.chars(g);
        }
      }
    }
    if (e == m) {
      throw "Parse Error: " + e;
    }
    m = e;
  }
  f();
};